package com.example.community.add;

import com.example.community.dao.RoomDao;
import com.example.community.dao.UnitDao;
import com.example.community.model.Room;
import com.example.community.model.Unit;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/roomAddServlet")
public class RoomAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        UnitDao unitDao = new UnitDao();
        List<Unit> units = unitDao.fandByAll();
        req.setAttribute("units",units);
        req.getRequestDispatcher("/WEB-INF/houseManager/roomAdd.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String rname = req.getParameter("rname");
            String unno = req.getParameter("unno");

            Room room = new Room();
            room.setrId(Integer.parseInt(rname));
            room.setUnNo(Integer.parseInt(unno));

            RoomDao roomDao = new RoomDao();
            Integer insert = roomDao.insert(room);
            if (insert == 1){
                resp.getWriter().write("true");
            }else {
                resp.getWriter().write("false");
            }
        }catch (Exception e){
            resp.getWriter().write("false");
        }

    }
}
